package mylist;
import java.util.Arrays;


public class MyArrayList implements IList{

    public int[] elem;
    public int usedSize;
    public static final int DEFAULT_CAPACITY = 5;

    public MyArrayList(){
        this.elem = new int[DEFAULT_CAPACITY];
    }
    public MyArrayList(int capacity){
        this.elem = new int[capacity];
    }

    @Override
    public void add(int data) {
        checkCapacity();
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    @Override
    public void add(int pos, int data) {
        try{
            checkPosOnAdd(pos);
        }catch(PosIllegality e){
            e.printStackTrace();
            return;
        }
        checkCapacity();
        for(int i = this.usedSize - 1; i >= pos; i--){
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
        if(isEmpty()){
            return false;
        }
        for(int i=0; i < this.usedSize; i++){
            if(this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    public boolean isEmpty(){
        return this.usedSize == 0;
    }

    @Override
    public int indexOf(int toFind) {
        if(isEmpty()){
            return -1;
        }
        for(int i=0; i < this.usedSize; i++){
            if(this.elem[i] == toFind){
                return i;
            }
        }
        return 0;
    }

    @Override
    public int get(int pos) {
        checkPosOnSet(pos);
        if(isEmpty()){
            throw new MyArrayListEmpty("获取指定下标元素时，顺序表为空");
        }
        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        checkPosOnSet(pos);
        this.elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        int index=indexOf(toRemove);
        if(index == -1){
            System.out.println("没有这个数字！");
            return;
        }
        for(int i=index; i < this.usedSize-1; i++){
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        this.usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }

    public boolean isFull(){
        return elem.length==usedSize;
    }

    public void checkCapacity(){
        if(isFull()){
            elem=Arrays.copyOf(elem,elem.length*2);
        }
    }

    public void checkPosOnAdd(int pos)throws PosIllegality{
        if(pos<0||pos>usedSize){
            System.out.println("下标pos不合法！！");
            throw new PosIllegality("插入元素下标异常: "+pos);
        }
    }

    public void checkPosOnSet(int pos)throws PosIllegality{
        if(pos<0||pos>usedSize){
            System.out.println("下标不合法：");
            throw new PosIllegality("插入元素下标异常: "+pos);
        }
    }
}
